package com.yd.app.mapper;

import com.yd.app.mapper.provider.GoodsProvider;
import com.yd.domain.business.Goods;
import com.yd.domain.business.GoodsPic;
import com.yd.domain.business.Pic;
import com.yd.domain.searchParam.GoodsSearchParam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author zg
 * @create 2018-03-07 17:45
 * @desc 商品
 **/
@Repository
public interface GoodsMapper {

    /**
     * 添加商品基础信息
     * @param goods
     */
    @Insert("insert into business_goods (id,price,perWeight,createTime,title,des,status,checkStatus,number,typeOne,type,ifPic,iconUrl,gstyle,identifier,px)" +
            " values(#{goods.id},#{goods.price},#{goods.perWeight},#{goods.createTime},#{goods.title},#{goods.des},'0'," +
            "#{goods.checkStatus},#{goods.number},#{goods.typeOne},#{goods.type},#{goods.ifPic},#{goods.iconUrl},#{goods.gstyle},#{goods.identifier},#{goods.px}) ")
    void addGoods(@Param("goods") Goods goods);

    /**
     * 修改产品状态
     * @param goodsId
     * @param status
     */
    @Update("update business_goods set status=#{status} where id=#{goodsId} ")
    void updateGoodsStatus(@Param("goodsId") String goodsId,@Param("status")  String status);

    /**
     * 修改产品状态
     * @param goodsId
     * @param pic_path
     */
    @Update("update business_goods set ifPic='1', iconUrl=#{pic_path} where id=#{goodsId} ")
    void updateIconUrl(@Param("goodsId") String goodsId,@Param("pic_path")  String pic_path);

    /**
     * 修改产品状态
     * @param id
     */
    @Update("update business_goods set ifPic='0', iconUrl='' where id=#{id} ")
    void deleteIconUrl(@Param("id") String id);

    /**
     * 审核产品状态
     * @param goodsId
     * @param checkStatus
     */
    @Update("update business_goods set checkStatus=#{checkStatus},checkUser=#{checkUser},checkTime=#{checkTime} where id=#{goodsId} ")
    void updateGoodsCheckStatus(@Param("goodsId") String goodsId, @Param("checkUser") String checkUser, @Param("checkTime") Date checkTime, @Param("checkStatus")  String checkStatus);

    /**
     * 修改商品
     * @param goods
     */
    @Update("update business_goods set price=#{goods.price},perWeight=#{goods.perWeight}," +
            "title=#{goods.title},des=#{goods.des},status=#{goods.status},updateUser=#{goods.updateUser},updateTime=#{goods.updateTime},number=#{goods.number}," +
            " typeOne=#{goods.typeOne}, type=#{goods.type},gstyle=#{goods.gstyle},identifier=#{goods.identifier},px=#{goods.px} where id=#{goods.id}")
    void updateGoods(@Param("goods") Goods goods);

    /**
     * 删除产品状态
     * @param goodsId
     */
    @Update("delete from business_goods where id=#{goodsId} ")
    void deleteGoodsById(@Param("goodsId") String goodsId);

    /**
     * 通过查询商品
     * @param goodsId
     * @return
     */
    @Select(" select o.*,dic.typeName as typeName,gs.typeName as gstyleName,p.title as pxName FROM business_goods o  LEFT JOIN business_gType dic ON dic.id=o.type and dic.goodKind='1'" +
            "  LEFT JOIN business_gstyle gs ON o.gstyle=gs.id " +
            " LEFT JOIN business_pinxing p on o.px=p.id " +
            " where o.id=#{goodsId}")
    Goods getGoodsById(@Param("goodsId") String goodsId);

    //查询商品数据
    @SelectProvider(method = "getGoodsList",type= GoodsProvider.class)
    List<Goods> getGoodsList(@Param("model")GoodsSearchParam param, @Param("startRow") int startRow, @Param("pageSize") int pageSize);

    //查询商品数量
    @SelectProvider(method = "getGoodsListCount",type= GoodsProvider.class)
    int getGoodsListCount(@Param("model")GoodsSearchParam param);

    //删除图片关联信息
    @Delete("delete from business_goods_pic where goodsId=#{goodsId} and picId=#{picId} ")
    void deleteGoodsImage(@Param("goodsId") String goodsId,@Param("picId") String picId);

    //删除图片数据信息
    @Delete("delete from business_pic where id=#{picId}")
    void deleteImageId(@Param("picId") String picId);

    //查询商品图片上数量
    @Select("select count(0) from business_goods_pic where goodsId=#{goodsId} ")
    int getGoodsImageCount(@Param("goodsId") String goodsId);

    //修改产品是否存在图片标识
    @Update("update business_goods set ifPic=#{b} where id=#{goodsId} ")
    void updateGoodsIfPic(@Param("goodsId") String goodsId,@Param("b") boolean b);

    //查询图片
    @Select("select * from business_pic where id=#{imageId} ")
    Pic getImageByID(@Param("imageId") String imageId);
    //查询商品图片
    @Select("select p.* from business_pic p where p.id IN (SELECT gp.picId from business_goods_pic gp where gp.goodsId=#{goodId})")
    List<Pic> getAllPic(@Param("goodId") String goodId);

    /**
     * 添加商品基础信息
     * @param goodsPic
     */
    @Insert("insert into business_goods_pic (goodsId,picId)" +
            " values(#{goodsPic.goodsId},#{goodsPic.picId}) ")
    void addGoodsPic(@Param("goodsPic") GoodsPic goodsPic);

    /**
     * 添加商品基础信息
     * @param pic
     */
    @Insert("insert into business_pic (id,picUrl)" +
            " values(#{pic.id},#{pic.picUrl}) ")
    void addPic(@Param("pic") Pic pic);
}
